function [ rsquare ] = Rsquare( rPortfolio, rIndex, beta )
%RSQUARE Summary of this function goes here
%   Detailed explanation goes here
    [m n] = size(rPortfolio);
    if (length(beta)==1)
        for i=1:n
            meanPort = mean(rPortfolio(:,i));
            sse(i) = sum((rPortfolio(:,i) - rIndex*beta).^2); 
            sst(i) = sum((rPortfolio(:,i) - meanPort).^2); 
        end
    else
        for i=1:n
            meanPort = mean(rPortfolio(:,i));
            sse(i) = sum((rPortfolio(:,i) - rIndex(i)*beta(i)).^2); 
            sst(i) = sum((rPortfolio(:,i) - meanPort).^2); 
        end
    end

    rsquare = 1 - sse/sst;
end

